package cn.edu.ctbu.service.impl;

import cn.edu.ctbu.domain.Rank;
import cn.edu.ctbu.domain.Resultss;
import cn.edu.ctbu.repository.ResultRepository;
import cn.edu.ctbu.repository.StudentRepository;
import cn.edu.ctbu.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class ResultServiceImpl implements ResultService {

    @Autowired
    private ResultRepository resultRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public java.util.List<Resultss> getAllResult() {
        return resultRepository.selectAllResult();
    }

    @Override
    public java.util.List<Resultss> selectByStuId(String stuId) {
        return resultRepository.selectResultByStuId(stuId);
    }

    @Override
    public java.util.List<Resultss> selectByStuIdAndResTerm(String stuId, String resTerm) {
        return resultRepository.selectResultByStuIdAndTerm(stuId,resTerm);
    }

    @Override
    public int addResult(Resultss resultss) {
        return resultRepository.insertResult(resultss);
    }

    @Override
    public int deleteResultById(int resId) {
        return resultRepository.deleteResultById(resId);
    }

    @Override
    public Resultss selectResultByStuIdAndSubName(String stuId, String subName) {
        return resultRepository.selectResultByStuIdAndSubName(stuId,subName);
    }

    @Override
    public Resultss selectResultByResId(int resId) {
        return resultRepository.selectResultByResId(resId);
    }

    @Override
    public java.util.List<Rank> selectRankByTerm(String resTerm) {
        java.util.List<Rank> ranks = resultRepository.selectRankByTerm(resTerm);
        for (Rank r : ranks) {
            Map<String, Integer> reamap=new HashMap<>();
            java.util.List<Map<String, Integer>> maps = resultRepository.selectResultMap(r.getStuId(), r.getResTerm());
            for (Map map:maps)
            {
                reamap.put((String)map.get("sub_name"),(Integer) map.get("res_num"));
            }
            r.setStuName(studentRepository.selectNameById(r.getStuId()));
            r.setResmap(reamap);
        }
        return ranks;
    }

    @Override
    public java.util.List<Rank> selectRankByTermAndStuClass(String resTerm, String stuClass) {
        java.util.List<Rank> ranks = resultRepository.selectRankByTermAndStuId(studentRepository.selectIdByClass(stuClass),resTerm);
        for (Rank r : ranks) {
            Map<String, Integer> reamap=new HashMap<>();
            java.util.List<Map<String, Integer>> maps = resultRepository.selectResultMap(r.getStuId(), r.getResTerm());
            for (Map map:maps)
            {
                reamap.put((String)map.get("sub_name"),(Integer) map.get("res_num"));
            }
            r.setStuName(studentRepository.selectNameById(r.getStuId()));
            r.setResmap(reamap);
        }
        return ranks;
    }
}
